(function(document, undefined){

Forum = window.Forum || {};

Forum.Edit = (function(){
  
  return {
    init: function() {
      $('#forum-key').change(function(e) {
        var $t = $(this).attr('disabled', true);
        var val = $t.val();
        
        if (val == '-1') {
          $('#forum-edit-form').get(0).reset();
          $t.attr('disabled', false);
          $('input[type=submit]').attr('disabled', true);
        } else {
          var $loading = $('<img src="/img/s16.gif" alt="loading" width="16" height="16">').insertAfter($t);
          $.getJSON('/rpc/forum', {method: 'getForumInfo', forum_key: val}, function(data) {
            $loading.remove();
            $t.attr('disabled', false);
            if (data.ok) {
              var f = data.forum;
              console.log(f);
              $('#forum-title').val(f.title);
              $('#forum-summary').val(f.summary);
              $('#forum-permalink').val(f.permalink);
              $('input[type=submit]').attr('disabled', false);
            } else {
              alert('讀取討論區資料發生錯誤');
            }
          });
        }
      });
    }
  };
  
})();

Forum.Edit.init();

})(document);
